package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.common.constant.Constant;
import com.sys.mapper.RoleMapper;
import com.sys.mapper.RolePermissionMapper;
import com.sys.pojo.Role;
import com.sys.pojo.RolePermission;
import com.sys.service.RoleService;
import com.sys.vo.RolePermissionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public IPage<Role> findListByPage(Role role) {
        IPage<Role> iPage = new Page<>(role.getPage(), role.getLimit());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Role::getDeleted, Constant.DELETED);
        queryWrapper.select(Role::getRoleName, Role::getId, Role::getCreateTime, Role::getDeleted, Role::getStatus);
        if (!StringUtils.isEmpty(role.getRoleName())) {
            queryWrapper.like(Role::getRoleName, role.getRoleName());
        }
        return roleMapper.selectPage(iPage, queryWrapper);
    }

    @Override
    public int add(Role role) {
        role.setDeleted(Constant.UNDELETED);
        return roleMapper.insert(role);
    }

    @Override
    public int delete(Integer id) {
        Role role = new Role();
        role.setId(id);
        role.setDeleted(Constant.DELETED);
        return roleMapper.updateById(role);
    }

    @Override
    public int updateData(Role role) {
        return roleMapper.updateById(role);
    }

    @Override
    public List<Role> listAll() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Role::getDeleted, Constant.DELETED);
        queryWrapper.select(Role::getId, Role::getRoleName);
        return roleMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRolePermission(RolePermissionVo vo) {
        List<Integer> pId = vo.getpId();
        int n = pId.size();
        int m = 0;
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, vo.getrId());
        rolePermissionMapper.delete(queryWrapper);
        for (Integer pid : pId) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(vo.getrId());
            rolePermission.setPermissionId(pid);
            m += rolePermissionMapper.insert(rolePermission);
        }
        if (m == n) {
            Set<String> keys = redisTemplate.keys("user" + "*");
            if (keys.size()!=0) {
                redisTemplate.delete(keys);
            }
            return true;
        } else {
            return false;
        }
    }


}
